import { observable, action, } from 'mobx';
import { SearchAdvanced, ListViewPage } from 'weaver-mobile-page';
import * as Api from '../../api/CommunicationApi';
import { PARAMS } from '../../util';


const initState = {
  joinLoading: true,
  joinAdValue: '',
  searchHrmid: null,
}
export default class HrmJoinStore {
  @observable search = new SearchAdvanced.Store({ route: '/hrmjion', id: 'adSearch' });
  @observable joinList = new ListViewPage.Store({ route:'/hrmjion', id: 'join' });
  @observable joinLoading = initState.joinLoading;
  @observable joinAdValue = initState.joinAdValue;
  @observable searchHrmid = initState.searchHrmid;

  @action setStoreState = (params) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    })
  }

  @action clear = () => this.setStoreState(initState)

  // 高级搜索模板请求
  getAdSearchCondition = (params) => {
    this.search.getData({
      url: '/api/cowork/base/getCoworkHrmViewShareCondition',
      method: `GET`,
      params: {...params,...PARAMS,}
    })
  }
  
  getJoinList = () => {
    let searchParams = this.search.form.formParams || {};
    let params = {
      searchHrmid: this.searchHrmid,
      ...searchParams
    }
    Api.getCoworkHrmViewList(params).then(res => {
      const dataKey = res.sessionkey;
      if (dataKey) {
        this.joinList.getData({ dataKey }, () => this.joinLoading = false, true);// ture是否刷新用
      }
    })
  }
  @action('高级列表重置数据')
  doJoinReset = () => {
    this.search.form.resetFields();
    this.joinAdValue = '';
    this.getJoinList();
  }

  @action('更新高级搜索字段')
  updateJoinAdvance = (val) => {
    this.joinAdValue = val;
    this.search.form.updateField('name', val);
  }
}